/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-04-07
*/
package com.rzico.basics.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.*;
import com.rzico.basics.model.CouponVo;
import com.rzico.basics.service.*;
import com.rzico.core.service.SysSequenceService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 * 全场活动控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-04-07
 */
@Api(description = "全场活动控制层")
@RestController
@RequestMapping("/promotion")
public class PromotionController extends BaseController {

    @Autowired
    private PromotionService promotionService;

    @Autowired
    private MemberPromotionService memberPromotionService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private SysSequenceService sysSequenceService;

    @Autowired
    private PromotionGiftService promotionGiftService;

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 获取首页弹窗活动
     *
     * @return
    */
    @ApiOperation("获取首页弹窗活动")
    @GetMapping("/popScreen")
    @ApiImplicitParams({@ApiImplicitParam(name = "mchId", value = "商户号", dataType = "String", paramType = "query")
    })
    public CommResult<Promotion> popScreen(String mchId, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        Enterprise enterprise = enterpriseService.findByMch(mchId);
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }
        params.put("type", 0);
        params.put("status", 1);
        params.put("deleted", 0);
        Member member = memberService.getCurrent();

        //用户是否参加过这个活动,是的话就不显示出活动
//        if (member!=null) {
//            params.put("memberPromotion", true);
//            params.put("memberId", member.getId());
//        }
        List<Promotion> list = promotionService.selectList(params);
        List<CouponVo> couponGifts = null;
        for (Promotion promotion:list)  {
            Map<String,Object> data = new HashMap<>();
            data.put("promotion",promotion);
//            if (promotion.getGiftType().equals(0)) {
//                data.put("itemList",promotionGiftService.getProductGift(promotion.getId()));
//            }
//            else {
                //根据promotion的id查询出这个活动的优惠券
                //List<CouponVo> couponGifts = promotionGiftService.getCouponGift(promotion.getId());
            if (member!=null) {
                couponGifts = promotionGiftService.getCouponGift2(promotion.getId(),member.getId());
            } else {
                couponGifts = promotionGiftService.getCouponGift(promotion.getId());
            }

            data.put("itemList",couponGifts);
  //          }
            //正常情况下没有活动
            boolean memberPromotionCoupon = false;
            if (couponGifts != null && couponGifts.size() != 0) {
                for (CouponVo couponGift : couponGifts) {
                    if (!couponGift.isReceived()) {
                        memberPromotionCoupon = true;
                    }
                }
            }

            if (!memberPromotionCoupon) {
                return CommResult.success("无活动");
            }
            return CommResult.success(data);
        }
        return CommResult.success("无活动");
    }


    /**
     * 获取扫码活动
     *
     * @return
     */
    @ApiOperation("获取扫码活动")
    @GetMapping("/scaner")
    @ApiImplicitParams({@ApiImplicitParam(name = "mchId", value = "商户号", dataType = "String", paramType = "query")
    })
    public CommResult<Promotion> scaner(String mchId, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        Enterprise enterprise = enterpriseService.findByMch(mchId);
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }
        params.put("type", 2);
        params.put("status", 1);
        params.put("expired", false);
        params.put("deleted", 0);

        List<Promotion> list = promotionService.selectList(params);
        for (Promotion promotion:list)  {
            Map<String,Object> data = new HashMap<>();
            data.put("promotion",promotion);
            if (promotion.getGiftType().equals(0)) {
                data.put("itemList",promotionGiftService.getProductGift(promotion.getId()));
            } else {
                data.put("itemList",promotionGiftService.getCouponGift(promotion.getId()));
            }
            return CommResult.success(data);
        }
        return CommResult.success("无活动");
    }

    /**
     * 点击领取
     *
     * @param promotionId
     * @return
     */
    @ApiOperation("点击领取")
    @PostMapping("/clickAndGet")
    public CommResult<Qrcode> clickAndGet(Long promotionId) {
        Member member = memberService.getCurrent();

        if (member==null) {
            return CommResult.logout();
        }

        List<CouponVo> couponGifts = null;
        //boolean memberPromotionCoupon = false;
        //创建一个list,存放这个活动红包的id
        List<CouponVo> couponVoList = new LinkedList<>();

        couponGifts = promotionGiftService.getCouponGift2(promotionId,member.getId());
        if (couponGifts != null && couponGifts.size() != 0) {
            for (CouponVo couponGift : couponGifts) {
                if (!couponGift.isReceived()) {
                    //memberPromotionCoupon = true;
                    couponVoList.add(couponGift);
                }
            }
        }

        if (couponVoList.size() <= 0) {
            return CommResult.success("已全部领取");
        }

        //领取未领取的优惠券
        promotionService.createUnreceivedGift(couponVoList,member,sysSequenceService.generate("couponCode"));
        return CommResult.success("已全部领取");
        //领取过了
//        if (memberPromotionService.checkExists(promotionId,member.getId())>0) {
////            return CommResult.error("您已经领取了");
//            couponGifts = promotionGiftService.getCouponGift2(promotionId,member.getId());
//            //正常情况下没有活动
//            if (couponGifts != null && couponGifts.size() != 0) {
//                for (CouponVo couponGift : couponGifts) {
//                    if (!couponGift.isReceived()) {
//                        //memberPromotionCoupon = true;
//                        couponVoList.add(couponGift);
//                    }
//                }
//            }
//            if (couponVoList.size() <= 0) {
//                return CommResult.success("已全部领取");
//            }
//
//            //领取未领取的优惠券
//            promotionService.createUnreceivedGift(couponVoList,member,sysSequenceService.generate("couponCode"));
//
//        } else {
//            Promotion promotion = promotionService.selectByPrimaryKey(promotionId);
//            if (promotion!=null) {
//                if (promotion.getGiftType().equals(0)) {
//                    return CommResult.error("此功能未开通");
//                }
//                if (promotion.getFirsted()!=null && promotion.getFirsted()==true) {
//                    if (member.getAmount().compareTo(BigDecimal.ZERO)>0) {
//                        return CommResult.error("新用户才能参与");
//                    }
//                }
//               promotionService.createGift(promotionId,member,sysSequenceService.generate("couponCode"));
//                return CommResult.success("领取成功");
//            } else {
//                return CommResult.success("手慢了,已结束");
//
//            }
//        }

    }


}
